package com.tudouji.project.farmer.controller;

import java.util.Date;
import java.util.List;

import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.project.system.domain.Tuser;
import com.tudouji.project.system.service.ITuserService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.farmer.domain.Farmerorder;
import com.tudouji.project.farmer.service.IFarmerorderService;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 种植订单Controller
 * 
 * @author tudouji
 * @date 2021-04-25
 */
@RestController
@RequestMapping("/farmer/farmerorder")
public class FarmerorderController extends BaseController
{
    @Autowired
    private IFarmerorderService farmerorderService;
    @Autowired
    private ITuserService tuserService;
    /**
     * 查询种植订单列表
     */
    @GetMapping("/getAll")
    public AjaxResult getAll(Farmerorder farmerorder)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        List<Farmerorder> list = null;
        try {
            list = farmerorderService.selectList(farmerorder, tuserId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success(list);
    }

    /**
     * 查询种植订单列表
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerorder:list')")
    @GetMapping("/list")
    public TableDataInfo list(Farmerorder farmerorder)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        List<Farmerorder> list = null;
        try {
            list = farmerorderService.selectFarmerorderList(farmerorder, tuserId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getDataTable(list);
    }

    /**
     * 导出种植订单列表
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerorder:export')")
    @Log(title = "种植订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Farmerorder farmerorder)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        List<Farmerorder> list = null;
        try {
            list = farmerorderService.export(farmerorder, tuserId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ExcelUtil<Farmerorder> util = new ExcelUtil<Farmerorder>(Farmerorder.class);
        return util.exportExcel(list, "farmerorder");
    }

    /**
     * 获取种植订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerorder:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(farmerorderService.selectFarmerorderById(guid));
    }

    /**
     * 新增种植订单
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerorder:add')")
    @Log(title = "种植订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Farmerorder farmerorder)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        farmerorder.setGuid(IdUtils.fastSimpleUUID());
        farmerorder.setCreatetime(new Date());
        farmerorder.setCreateTime(new Date());
        farmerorder.setOperatorid(tuserId);
        Tuser tuser1 = tuserService.selectTuserById(farmerorder.getOrderownerid());
        farmerorder.setOrderownername(tuser1.getRealname());
        Tuser tuser2 = tuserService.selectTuserById(farmerorder.getPlantownerid());
        farmerorder.setPlantownername(tuser2.getRealname());
        return toAjax(farmerorderService.insertFarmerorder(farmerorder));
    }

    /**
     * 修改种植订单
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerorder:edit')")
    @Log(title = "种植订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Farmerorder farmerorder)
    {
        String tuserId = SecurityUtils.getLoginUser().getUser().getTuserId();
        farmerorder.setOperatorid(tuserId);
        farmerorder.setLastupdate(new Date());
        Tuser tuser1 = tuserService.selectTuserById(farmerorder.getOrderownerid());
        farmerorder.setOrderownername(tuser1.getRealname());
        Tuser tuser2 = tuserService.selectTuserById(farmerorder.getPlantownerid());
        farmerorder.setPlantownername(tuser2.getRealname());
        return toAjax(farmerorderService.updateFarmerorder(farmerorder));
    }

    /**
     * 删除种植订单
     */
    @PreAuthorize("@ss.hasPermi('farmer:farmerorder:remove')")
    @Log(title = "种植订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(farmerorderService.deleteFarmerorderByIds(guids));
    }
}
